<?php defined('SYSPATH') or die('No direct script access.');
  
class Controller_Frontend_Show extends Controller_Frontend_Setfrontend
{
	private $model;
	
	public function before(){
		parent::before();
		$this->model = new Model_Cms_Show;
    }
	
    /**
	 * strona startowa
	 * @return view startpage
	 */
	public function action_start()
	{
		$view = View::factory('frontend/startpage');
		
		$this->template->content = $view;
	}
	
	public function action_showpage($id)
	{
		$view = View::factory('frontend/pages_show');
		$model = new Model_Cms_Pages;
		$page = $model->get_page($id);
		$view->page = $page;
		
		$dodane = $this->model->last_added_right();
		$view->dodane = $dodane;
		// tutaj warunki do wyświetlania SEO - ze strony albo globalne
		$this->template->content = $view;
		
	}
	
	public function action_search()
	{
		$key = trim(Security::xss_clean($_GET['key']));
		$res_ids = array();
		// wyszukiwanie w tablicy videos
		$pages = DB::Select()->from('videos');
		$pages->where('name','like','%'.$key.'%');
		$pages->or_where('text_stripped','like','%'.$key.'%');
		$pages->and_where('published','=',1);
		$res_video = $pages->execute();
		
		$i=0;
		foreach($res_video as $rv)
		{
			$res_ids[$i]['id'] = $rv['id'];
			$res_ids[$i]['type'] = 'video';
			$res_ids[$i]['link'] = 'video/'.$rv['id'];
			$res_ids[$i]['title'] = $rv['name'];
			$i++;
		}
		
		// wyszukiwanie w tablicy pages
		$pages = DB::Select()->from('pages');
		$pages->where('title','like','%'.$key.'%');
		$pages->or_where('content_stripped','like','%'.$key.'%');
		$pages->or_where('short_stripped','like','%'.$key.'%');
		$pages->and_where('published','=',1);
		$res_pages = $pages->execute();
		
		foreach($res_pages as $rp)
		{
			if($rp['category'] == 0)
			{
				$res_ids[$i]['id'] = $rp['id'];
				$res_ids[$i]['type'] = 'page';
				$res_ids[$i]['title'] = $rp['title'];
				if($rp['category_id'] == 2)
					$res_ids[$i]['link'] = 'aktualnosci/'.$rp['id'];
				else if ($rp['category_id'] == 4) $res_ids[$i]['link'] = 'analizy/'.$rp['id'];
				else $res_ids[$i]['link'] = '';
				$i++;
			}
		}
		//echo Kohana::debug($res_ids);exit;
		$view = View::factory('frontend/frontend_search_results');
		$view->results = $res_ids;
		// liczba rezultatow + odpowedni przypadek rzeczownika 'rezultat'
		$count = count($res_ids);
		if($count == 1) $view->found = '<strong'.$count.'</strong> rezultat';
		else if($count > 1 && $count < 5) $view->found = '<strong>'.$count.'</strong> rezultaty';
		else if($count > 4) $view->found = '<strong>'.$count.'</strong> rezultatów';
		$this->template->content = $view;
	}
	
	public function action_mostviewed()
	{
		$view = View::factory('frontend/frontend_most_viewed');
		$videos = $this->model->videos_most_viewed(10);
		$view->video = $videos[0];
		$podobne = $this->model->videos_podobne($videos[0]['video_id'],5);
		$view->podobne = $podobne;
		//array_shift($videos);
		$view->videos = $videos;
		//echo Kohana::debug($videos);exit;
		$this->template->content = $view;
	}
	
	public function action_weekgoal()
	{
		$view = View::factory('frontend/frontend_week_goal');
		$videos = $this->model->get_videos_goal(10);
		$view->video = $videos[0];
		$podobne = $this->model->videos_podobne($videos[0]['id'],5);
		$view->podobne = $podobne;
		//array_shift($videos);
		$view->videos = $videos;
		//echo Kohana::debug($videos);exit;
		$this->template->content = $view;
	}
	
	public function action_ligues()
	{
		$view = View::factory('frontend/frontend_ligues');
		$last = $this->model->video_last_added();
		$view->video = $last;
		$podobne = $this->model->videos_podobne($last['id'],5);
		$view->podobne = $podobne;
		// lista lig
		$ligi = $this->model->get_ligues();
		$view->ligi = $ligi;
		//echo Kohana::debug($ligi);exit;
		$this->template->content = $view;
	}
	
	public function action_kolejka($id)
	{
		$view = View::factory('frontend/frontend_kolejka');
		$videos = $this->model->get_kolejka($id);
		$view->videos = $videos;
		$view->video = $videos[0];
		$podobne = $this->model->videos_podobne($videos[0]['id'],5);
		$view->podobne = $podobne;
		$nazwa = $this->model->get_kolejka_liga_name($videos[0]['id']);
		$view->nazwa = $nazwa;
		// paginacja - nastepne / poprzednie kolejki
		$view->next = $this->model->pagination_kolejka($id,'next');
		$view->prev = $this->model->pagination_kolejka($id,'prev');
		//echo Kohana::debug($pagination);exit;
		$this->template->content = $view;
	}
	
	public function action_analizalist()
	{
		$view = View::factory('frontend/frontend_analizy_lista');
		$dodane = $this->model->last_added_right();
		$view->dodane = $dodane;
		
		$count = $this->model->count_analizy();
		$pagination = Pagination::factory(array(
					'total_items'    => $count,
					'items_per_page' => 6
				));
		$analizy = $this->model->analizy_lista($pagination->items_per_page,$pagination->offset);
		$view->analizy = $analizy;
		$view->page_links = $pagination->render();
		
		//echo Kohana::debug($analizy);exit;
		$this->template->content = $view;
	}
}